/**
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.ce.pms.house.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IAreaDao;
import com.ce.pms.house.dao.ICoWorkerDao;
import com.ce.pms.house.dao.IImageDao;
import com.ce.pms.house.model.AreaDomain;
import com.ce.pms.house.model.BuildingDomain;
import com.ce.pms.house.model.CoWorkerDomain;
import com.ce.pms.house.model.ImageDomain;
import com.ce.pms.house.model.Layout;
import com.ce.pms.house.model.ProjectDomain;
import com.ce.pms.house.service.IAreaService;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.ICoWorkerService;
import com.ce.pms.house.service.IImageService;
import com.ce.pms.house.service.IProjectService;
import com.ce.pms.house.vo.AreaVO;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.ImageVO;
import com.ce.pms.house.vo.ProjectVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述: AreaServiceImpl 楼盘业务处理类
 *
 * @author  付湘辉
 * Created on：  2008-6-25
 * @author  付湘辉
 * update on：  2008-6-25
 */

public class AreaServiceImpl extends PmsBaseServiceImpl implements IAreaService {

	/**
	 * 楼盘Dao注入
	 */
	private IAreaDao areaDao;


	/**
	 * 楼栋Service注入
	 */
	private IBuildingService buildingService;

	private ICoWorkerDao coWorkerDao;

	private ICoWorkerService coWorkerService;

	private IImageDao imageDao;

private IImageService imageService;

	private IProjectService projectService;

	/**
	 * 方法描述: 增加楼盘图片资料
	 * @author 胡煦
	 * Create on:2008-7-1
	 * @param areaVO
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ImageVO addOrUpdateImage(ImageDomain imageDomain){

		return null;
	}
	/**
	 * 方法描述: Do to Vo
	 * @author 胡煦
	 * Create on:2008-6-26
	 * @param AreaDomain
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public AreaVO assembleD2V(AreaDomain areaDo){
		super
		.checkParameterIsNull(areaDo,
				"common:parameters_null_exception, ");
		AreaVO areaVo = new AreaVO();
		areaVo.setAddress(areaDo.getAddress());
		areaVo.setAreaCreator(areaDo.getSysInfo() == null ? null : areaDo
				.getSysInfo().getCreator());
		areaVo.setKeyword(areaDo.getKeyword());
		areaVo.setAreaModifier(areaDo.getSysInfo() == null ? null : areaDo
				.getSysInfo().getModifier());
		areaVo.setAreaModifyDate(DateUtil.convertDateToStr((Timestamp) areaDo
				.getSysInfo().getModifyDate(), "yyyy-MM-dd"));
		areaVo.setName(areaDo.getName());
		areaVo.setAreaSysDate(DateUtil.convertDateToStr((Timestamp) areaDo
				.getSysInfo().getSysDate(), "yyyy-MM-dd"));
		areaVo.setBottomOrg(coWorkerService.getCoWorkerVOByCoWorkerDomain(areaDo.getBottomOrg()== null ? null :areaDo.getBottomOrg()));
		areaVo.setHasMoreProject(areaDo.getHasMoreProject());
		areaVo.setLayout(projectService.assembleD2V(areaDo.getLayout()== null ? null :areaDo.getLayout()));
		areaVo.setTrafficImage(imageService.convertDomainToVo( areaDo.getTrafficImage()==null ? null :areaDo.getTrafficImage())  );
		areaVo.setPoliceStation(coWorkerService.getCoWorkerVOByCoWorkerDomain(areaDo.getPoliceStation()== null ? null :areaDo.getPoliceStation()) );
		areaVo.setStreetOrg(coWorkerService.getCoWorkerVOByCoWorkerDomain(areaDo.getStreetOrg()== null ? null :areaDo.getStreetOrg())) ;

		return this.assembleV2V(areaVo, this.ifHasProject(areaDo));
	}

	/**
	 * 方法描述: 修改楼盘基本资料
	 * @author 胡煦
	 * Create on:2008-7-1
	 * @param areaVO
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public AreaVO editBase(AreaVO areaVO) {

		super.checkParameterIsNull(areaVO,"common:parameters_null_exception, ");

		AreaDomain ad = areaDao.findById(areaVO.getKeyword());
		ad.setAddress(areaVO.getAddress());
		ad.setHasMoreProject(areaVO.getHasMoreProject());

		//判断是否增加默认期数
		if("0".equals(ad.getHasMoreProject())){
			this.buildDefaultProject(ad);
		}

		//ad.setName(areaVO.getName());
		SysInfo sysInfo = ad.getSysInfo();
		modifyInfo(sysInfo,areaVO.getAreaModifier());
		ad.setSysInfo(sysInfo);
		areaVO.setName(ad.getName());

		ImageDomain  imageDomain = imageService.getPossibleImageDomain(areaVO.getTrafficImage(),
														areaVO.getTrafficImage().getCreator());
		ad.setTrafficImage(imageDomain);
		return areaVO;
	}

	/**
	 * 方法描述: 修改楼盘政府关系资料
	 * @author 胡煦
	 * Create on:2008-7-1
	 * @param areaVO
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public AreaVO editGovtRelation(AreaVO areaVO) {
		super
		.checkParameterIsNull(areaVO,
				"common:parameters_null_exception, ");
		if(BlankUtil.isBlank(areaVO.getBottomOrg())){
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			areaDomain.setBottomOrg(null);
			areaVO.setBottomOrg(null);

		}else {
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			CoWorkerDomain bottomOrg = coWorkerDao.findById(areaVO.getBottomOrg().getCoWorkerID());
			areaDomain.setBottomOrg(bottomOrg);
			areaVO.setBottomOrg(coWorkerService.getCoWorkerVOByCoWorkerDomain(bottomOrg));
		}
		if(BlankUtil.isBlank(areaVO.getPoliceStation())){
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			areaDomain.setPoliceStation(null);
			areaVO.setPoliceStation(null);
		}else {
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			CoWorkerDomain policeStation = coWorkerDao.findById(areaVO.getPoliceStation().getCoWorkerID());
			areaDomain.setPoliceStation(policeStation);
			areaVO.setPoliceStation(coWorkerService.getCoWorkerVOByCoWorkerDomain(policeStation));
		}
		if(BlankUtil.isBlank(areaVO.getStreetOrg())){
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			areaDomain.setStreetOrg(null);
			areaVO.setStreetOrg(null);

		}else {
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			CoWorkerDomain streetOrg = coWorkerDao.findById(areaVO.getStreetOrg().getCoWorkerID());
			areaDomain.setStreetOrg(streetOrg);
			areaVO.setStreetOrg(coWorkerService.getCoWorkerVOByCoWorkerDomain(streetOrg));
		}
		return areaVO;
	}

	/**
	 * 方法描述: 修改楼盘建设规划资料
	 * @author 胡煦
	 * Create on:2008-7-1
	 * @param areaVO
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public AreaVO editLayout(AreaVO areaVO) {
		super
		.checkParameterIsNull(areaVO,
				"common:parameters_null_exception, ");
		if(BlankUtil.isBlank(areaVO.getLayout().getKeyword())){
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			Layout layout = projectService.assembleV2D(areaVO.getLayout());
			areaDomain.setLayout(layout);
			areaVO.setLayout(projectService.assembleD2V(areaDomain.getLayout()));
			return areaVO;
		}else {
			AreaDomain areaDomain = areaDao.findById(areaVO.getKeyword());
			Layout layout = areaDomain.getLayout();
			Layout layoutNew = projectService.assembleV2D(areaVO.getLayout());
			PopulateUtil.populate(layoutNew, layout);
			areaVO.setLayout(projectService.assembleD2V(areaDomain.getLayout()));

			return areaVO;
		}
	}
	/**
	 * 方法描述: 根据楼盘ID查找楼盘VO
	 * @author 胡煦
	 * Create on:2008-6-26
	 * @param id
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */

	public AreaVO findAreaById(Long areaId) {
		super
		.checkParameterIsNull(areaId,
				"common:parameters_null_exception, ");
		return this.assembleD2V(areaDao.findById(areaId));
	}
	/**
	 * 方法描述: 1. 当hasMoreProject=0时,调用IProjectService,查询当前楼盘的期数资料. 2.
	 * 当期数资料数据大于1笔时,异常提示"请先删除原有期数资料,再来修改".
	 *
	 * @author 胡煦 Create on:2008-7-1
	 * @param AreaVO
	 * @return Edit author: Edit on: 修改原因：
	 */
	public boolean ifAllowMerge(AreaVO o) {
		if(BlankUtil.isBlank(o)){
			return false;
		}else {
			o = this.findAreaById(o.getKeyword());
			if(o.getProjectVO().size() > 1){
				throw new BusinessException("house:house_must_delete_exception");
			}else
				return true;
			}
	}

	/**
	 * 方法描述:1. 当hasMoreProject=0时,调用IProjectService查询当前楼盘的期数资料.
	2. 如果期数资料数据大于1笔时,异常提示用户"数据有误, 请联系系统管理员."
	3. 当hasMoreProject=1时,返回null.
	 * @author 胡煦
	 * Create on:2008-6-30
	 * @param AreaVO
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
		public Map<Long, ProjectVO> ifHasProject(AreaDomain areaDomain) {
			super.checkParameterIsNull(areaDomain,
					"common:parameters_null_exception, ");
			if ("0".equals(areaDomain.getHasMoreProject())) {
				if (areaDomain.getProjects().size() > 1) {
					throw new BusinessException(
							"house:house_number_error_exception");
				}
			}
			Map<Long, ProjectVO> projectVO = new HashMap<Long, ProjectVO>();
			for (ProjectDomain p : areaDomain.getProjects()) {
				projectVO
						.put(p.getKeyword(), projectService.assembleD2V(p));
			}
			return projectVO;
		}

	/**
	 *
	 * 功能描述: 得到所有楼盘及其下的楼栋资料的树列表。
	 * 将每一个楼盘和其下的楼栋列表放入一个Map中，再将Map添加进一个列表，用于返回。
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：annotation
	 * @return List<Map> 返回结果
	 */
	public List<Map> queryAreaBuildingTreeList(){
		List<Map> list = new ArrayList<Map>(1);
		Map<Object, List<BuildingVO>> areaBuildMap = new HashMap<Object, List<BuildingVO>>();

		//得到所有楼盘信息
		List<AreaDomain> areaList = areaDao.findAllResult("from AreaDomain Area_v", null);

		AreaVO areaVo = null;
		BuildingVO buildVo = null;
		List<BuildingVO> buildVOList = null;

		if (BlankUtil.isBlank(areaList)) {
			return list;
		}

		for (AreaDomain domain : areaList) {
			areaBuildMap = new HashMap<Object, List<BuildingVO>>();

			areaVo = new AreaVO();// this.assembleD2V(domain);
			areaVo.setKeyword(domain.getKeyword());
			areaVo.setName(domain.getName());
			areaVo.setHasMoreProject(domain.getHasMoreProject());

			/*Set<BuildingDomain> buildSet = domain.getBuildings();
			buildVOList = new ArrayList<BuildingVO>();
			if (!BlankUtil.isBlank(buildSet)) {

				for (BuildingDomain buildDomain : buildSet) {
					buildVo = this.buildingService.assembleD2V(buildDomain);
					buildVOList.add(buildVo);
				}

				//BuildingVO已实现compareTo接口,添加自身逻辑,在比较时会用BuildingVO.seqNo进行比较,升序排列
				Collections.sort(buildVOList);
			}*/
			List<BuildingDomain> buildDoList = this.buildingService.getBuildingDoListByAreaId(domain.getKeyword());
			buildVOList = new ArrayList<BuildingVO>();
			if (!BlankUtil.isBlank(buildDoList)) {

				for (BuildingDomain buildDomain : buildDoList) {
					buildVo = this.buildingService.assembleD2V(buildDomain);
					buildVOList.add(buildVo);
				}

				//BuildingVO已实现compareTo接口,添加自身逻辑,在比较时会用BuildingVO.seqNo进行比较,升序排列
				Collections.sort(buildVOList);
			}
			areaBuildMap.put(areaVo, buildVOList);

			list.add(areaBuildMap);
		}

		return list;
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.IAreaService#queryAreas(com.ce.pms.house.vo.AreaVO, int, int)
	 */
	public List<AreaVO> queryAreas(AreaVO o, int pageNo, int pageSize) {

		return null;
	}

	/**
	 * 功能描述: 设置楼盘Dao
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：
	 * @param areaDao the 楼盘Dao to set
	 */
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	/**
	 * 功能描述: 设置楼栋Service
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：
	 * @param buildingService the 楼栋Service to set
	 */
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @param coWorkerDao the coWorkerDao to set
	 */
	public void setCoWorkerDao(ICoWorkerDao coWorkerDao) {
		this.coWorkerDao = coWorkerDao;
	}

	/**
	 * @param coWorkerService the coWorkerService to set
	 */
	public void setCoWorkerService(ICoWorkerService coWorkerService) {
		this.coWorkerService = coWorkerService;
	}

	/**
	 * @param imageDao the imageDao to set
	 */
	public void setImageDao(IImageDao imageDao) {
		this.imageDao = imageDao;
	}


	/**
	 * @param imageService the imageService to set
	 */
	public void setImageService(IImageService imageService) {
		this.imageService = imageService;
	}

	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	/**
	 * 方法描述: set ProjectVO into AreaVO
	 * @author 胡煦
	 * Create on:2008-6-26
	 * @param AreaVO , ProjectVO
	 * @return AreaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private AreaVO assembleV2V(AreaVO areaVO,Map<Long, ProjectVO> projectVO){
		if (BlankUtil.isBlank(areaVO)||BlankUtil.isBlank(projectVO)) {
			return areaVO;
		}
		areaVO.setProjectVO(projectVO);
		return  areaVO;
	}

	/**
	 * 方法描述: 1. 当hasMoreProject=0时且楼盘关联的期资料大 于1笔时,异常提示用户"数据有误,请联系系统管理 员".
	 *          2. 当hasMoreProject=0时且楼盘关联期数资料笔数等 于0时,调用IProjectService创建一笔期数资料.
	 *          3. 当hasMoreProject=0时且楼盘关联期数资料笔数等 于1时,不做任何操作.
	 * @author 胡煦
	 * Create on:2008-7-10
	 * @param AreaDomain
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private ProjectVO buildDefaultProject(AreaDomain area){
		super
		.checkParameterIsNull(area,
				"common:parameters_null_exception, ");
		if("0".equals(area.getProjects().size())){
			ProjectVO projectVO = new ProjectVO();
			projectVO.setAreaId(area.getKeyword());
			projectVO.setName("默认期");
			projectService.addBase(projectVO);
			return projectVO;
		}else if(1 < area.getProjects().size()){
			throw new BusinessException("house:house_number_error_exception");
		}
		return null;
	}

}
